package com.smooth.site.sx.pc.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.smooth.common.pc.interceptor.PcAccountContext;
import com.smooth.common.site.common.MD5PawService;
import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.utils.StringUtils;
import com.smooth.common.site.common.utils.UserAgentUtils;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.dao.gen.bean.BizStudent;
import com.smooth.site.sx.pc.company.enums.JobFlagEnums;
import com.smooth.site.sx.pc.company.service.PcCompService;
import com.smooth.site.sx.pc.jifen.service.PcJifenService;
import com.smooth.site.sx.pc.student.service.PcStudentService;

public class PcAuthInterceptor  extends HandlerInterceptorAdapter {
	private Logger log = LoggerFactory.getLogger(PcAuthInterceptor.class);
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		
		String token = request.getHeader("token");
		String telNo = request.getHeader("telNo");
		String regType = request.getHeader("regType");
		if(StringUtils.isBlank(telNo)) {
			telNo = request.getParameter("telNo");
		}
		
		if(StringUtils.isBlank(token)) {
			token = request.getParameter("token");
		}
		if(StringUtils.isBlank(regType)) {
			regType = request.getParameter("regType");
		}
		if(StringUtils.isBlank(telNo)) {
			telNo = request.getParameter("accountNo");
		}
		
		if(UserAgentUtils.isMobileOrTablet(request)) {
			PcAccountContext.setMobile(true);
		}else {
			PcAccountContext.setMobile(false);
		}
		
		log.info("token is "+token);
		log.info("telNo is "+telNo);
		log.info("MD5PawService is "+MD5PawService.isMd5Psw(telNo,token));
		String reqUrl = request.getRequestURL().toString();
		if(isNeedAuth(request)) {
			if((StringUtils.isBlank(token) ||StringUtils.isBlank(telNo) || !MD5PawService.isMd5Psw(telNo,token))) {
				log.info("登录错误");
				throw new SiteException(SiteCodeEnums.LOGIN_FAILED,"请重新登录");
			}
			if(StringUtils.isNoneBlank(token) && MD5PawService.isMd5Psw(telNo,token)) {
				PcAccountContext.setLoginInfo(token, telNo);
			}
			
			setLoginObj(regType);
		}else {
			if(StringUtils.isNoneBlank(token) && MD5PawService.isMd5Psw(telNo,token)) {
				PcAccountContext.setLoginInfo(token, telNo);
			}
		}
		return true;
	}
	@Autowired
	PcCompService pcCompService;
	@Autowired
	PcStudentService pcStudentService;
	@Autowired
	PcJifenService pcJifenService;
	private void setLoginObj(String regType) {
		if(StringUtils.isBlank(regType)) {
			throw new SiteException(SiteCodeEnums.param_error,"缺少regType");
		}
		if("2".equals(regType)) {
			//企业登录
			BizCompany bizCompany = pcCompService.queryCompByTelNo(PcAccountContext.getTelNo());
			if(bizCompany==null) {
				throw new SiteException(SiteCodeEnums.LOGIN_FAILED, PcAccountContext.getTelNo());
			}
			
			if(!JobFlagEnums.nomal.value.equals(bizCompany.getJobFlag())) {
				throw new SiteException(SiteCodeEnums.STATUS_FAILED, PcAccountContext.getTelNo()+" 请联系管理员");
			}
			PcAccountContext.setObj(BizCompany.class, bizCompany);
		}else if("1".equals(regType)) {
			//学生登录
			BizStudent bizStudent = pcStudentService.queryByTelNo(PcAccountContext.getTelNo());
			if(bizStudent==null) {
				throw new SiteException(SiteCodeEnums.LOGIN_FAILED, PcAccountContext.getTelNo());
			}
			//添加积分
			pcJifenService.loginAddJifen(bizStudent, 10);
			PcAccountContext.setObj(BizStudent.class, bizStudent);
		}
	}

	private boolean isNeedAuth(HttpServletRequest request) {
		String requri = request.getRequestURL().toString();
		if(!requri.contains("/p/")) {
			return true;
		}
		return false;
	}
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		PcAccountContext.remove();
		super.postHandle(request, response, handler, modelAndView);
	}
}
